package com.new2i.service.impl;
   
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; 
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
  
import com.new2i.dao.able.ScreenDoctorDao; 
import com.new2i.model.DoctorEntity; 
import com.new2i.model.ScreenDoctorEntity; 
import com.new2i.service.able.ScreenDoctorService;
import com.new2i.util.DateFormatUtil;
import com.new2i.util.RegexUtil;
import com.new2i.util.SmsSendUtil; 
/**
 * 筛查医生的业务逻辑处理实现类
 * 
 * @since 2015/09/22
 * @author 2i
 *
 */
@Service("ScreenDoctorService")
public class ScreenDoctorServiceImpl implements ScreenDoctorService {
 
	private static Logger logger = Logger.getLogger(ScreenDoctorServiceImpl.class);
 
	@Autowired
	private ScreenDoctorDao mScreenDoctorDao;
   
	@Override
	public List<HashMap<String, Object>> selectScreenDoctorList(DoctorEntity doctor) {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		Integer doctor_id = doctor.getDoctor_id();
		Integer page = doctor.getPage();
		if(page == null)
			page = 1;
		if(doctor_id == null || doctor_id == 0) { 
			return list;
		} 
		try { 
			List<HashMap<String, Object>> scDoctorList = mScreenDoctorDao.selectScreenDoctorList(doctor_id, page);
			return scDoctorList;
		}catch (Exception e){
			logger.error(e.getLocalizedMessage()); 
		} 
		return list;
	}

	@Override
	public HashMap<String, Object> insertScreenDoctor(ScreenDoctorEntity scDoctor) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>();
		String tel = scDoctor.getTel();  
		if(tel == null) {
			rHashMap.put("code", 312);
			rHashMap.put("err_msg", "未填写电话号码");
			return rHashMap;
		}else if(!RegexUtil.validatePhoneNum(tel)){
			rHashMap.put("code", 313);
			rHashMap.put("err_msg", "请输入合法的电话号码");
			return rHashMap;
		}
		try { 
			Date now = new Date();  
			scDoctor.setExist_status(0);
			scDoctor.setCreate_date(now); 
			scDoctor.setUpdate_date(now);
			scDoctor.setPassword("123456"); 
			ScreenDoctorEntity rScDoctor = mScreenDoctorDao.insertScreenDoctor(scDoctor);
			//发送通知短信
			StringBuffer sb = new StringBuffer("您已经成注册2i筛查账号!\r\n您的登录名是:");
			sb.append(tel);
			sb.append("\r\n登录密码:");
			sb.append(scDoctor.getPassword());
			sb.append("\r\n请访问网站: http://kmeye.vt2020.com 点击左上角【客户端下载】下载最新客户端。");
			sb.append("\r\n恭喜您可以开始进行筛查工作了!");
			sb.append("\r\n如有疑问请致电153-6799-3568"); 
			String content = sb.toString();
			SmsSendUtil.sendSms(tel, content);
			rHashMap.put("sc_doctor_id", rScDoctor.getSc_doctor_id());  
			return rHashMap;
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "创建筛查医生失败");
		} 
		return rHashMap;
	}

	@Override
	public HashMap<String, Object> updateScreenDoctorPassword(ScreenDoctorEntity scDoctor) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer scDoctorId = scDoctor.getSc_doctor_id();
		String password = scDoctor.getPassword();
		String newPassword = scDoctor.getNew_password(); 
		String tel = scDoctor.getTel();  
		if(scDoctorId == null || scDoctorId == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查医生"); 
			return rHashMap;
		} 
		if(password == null || password.length() <= 0) {
			rHashMap.put("code", 312);
			rHashMap.put("err_msg", "请输入原密码"); 
			return rHashMap; 
		}
		if(newPassword == null || newPassword.length() <= 0) {
			rHashMap.put("code", 313);
			rHashMap.put("err_msg", "新密码不能为空"); 
			return rHashMap;
		}
		if(password.equals(newPassword)) {
			rHashMap.put("code", 314);
			rHashMap.put("err_msg", "新密码与原密码相同"); 
			return rHashMap;
		}
		try {  
			Integer updateRows = mScreenDoctorDao.updateScreenDoctorPassword(scDoctorId, password, newPassword); 
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", scDoctorId); 
				rHashMap.put("msg", "密码修改成功"); 
				if(RegexUtil.validatePhoneNum(tel)) { 
					String sTime = DateFormatUtil.getInstance().getCurrentDateTime();
					StringBuffer sb = new StringBuffer("您于"); 
					sb.append(sTime);
					sb.append("修改了2i筛查医生账号【"); 
					sb.append(tel);
					sb.append("】的登录密码\r\n新密码是:"); 
					sb.append(scDoctor.getNew_password());
					sb.append("\r\n请妥善保管好您的账号和密码");
					sb.append("\r\n若非本人操作,请重新修改密码,提升账号安全保护!");
					sb.append("\r\n如有疑问请致电153-6799-3568"); 
					String content = sb.toString();
					SmsSendUtil.sendSms(tel, content);
				}
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "原密码错误"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "密码修改失败");
		} 
		return rHashMap; 
	} 
	
	@Override
	public HashMap<String, Object> deleteScreenDoctor(ScreenDoctorEntity scDoctor) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer scDoctorId = scDoctor.getSc_doctor_id();
		Integer doctorId = scDoctor.getDoctor_id(); 
		if(doctorId == null || doctorId == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定当前机构操作医生"); 
			return rHashMap;
		}  
		if(scDoctorId == null || scDoctorId == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查医生"); 
			return rHashMap;
		}   
		try {  
			Integer updateRows = mScreenDoctorDao.deleteScreenDoctor(scDoctorId,doctorId); 
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", scDoctorId); 
				rHashMap.put("msg", "删除筛查医生成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "筛查医生已被删除"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "删除筛查医生出错");
		} 
		return rHashMap; 
	}  
}
